package ie.live.byrnep.pleasesleephere.bookroom;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import ie.live.byrnep.pleasesleephere.R;
import ie.live.byrnep.pleasesleephere.base.CloudConnection;
import ie.live.byrnep.pleasesleephere.base.MessageDetails;
import ie.live.byrnep.pleasesleephere.bookroom.MainActivity.RetrieveRooms;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Room extends Activity {
	private CloudConnection cloudConnection;
	private Intent intent;
	private TextView titleTextView;
	private TextView descriptionTextView;
	private TextView priceTextView;
	private TextView houseTypeTextView, roomTypeTextView, bathRoomNoTextView, smokingTextView, 
					 preferredAgeTextView, preferredGenderTextView, tvTextView, sky_cableTextView,
					 onsuiteTextView, internetTextView, heatingTextView, air_conTextView;
	private Button messageButton;
	private String entityKey;
	private ImageView imageView;
	private Map<String, Object> roomMap;
	private Bitmap thumbnailImage;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_room);
		cloudConnection = new CloudConnection(this);
		titleTextView = (TextView) findViewById(R.id.titleTextView);
		descriptionTextView = (TextView) findViewById(R.id.messageTextView);
		priceTextView = (TextView) findViewById(R.id.priceTextView);
		
		houseTypeTextView = (TextView) findViewById(R.id.house_typeTextView);
		roomTypeTextView = (TextView) findViewById(R.id.room_typeTextView);
		bathRoomNoTextView = (TextView) findViewById(R.id.bathroom_countTextView);
		smokingTextView = (TextView) findViewById(R.id.smokingTextView);
		preferredAgeTextView = (TextView) findViewById(R.id.ageTextView);
		preferredGenderTextView = (TextView) findViewById(R.id.genderTextView);
		tvTextView = (TextView) findViewById(R.id.tvTextView);
		sky_cableTextView = (TextView) findViewById(R.id.sky_cableTextView);
		onsuiteTextView = (TextView) findViewById(R.id.onsuiteTextView);
		internetTextView = (TextView) findViewById(R.id.internetTextView);
		heatingTextView = (TextView) findViewById(R.id.heatingTextView);
		air_conTextView = (TextView) findViewById(R.id.air_comTextView);
		imageView = (ImageView) findViewById(R.id.img);
		messageButton = (Button) findViewById(R.id.messageButton);
		
		Bundle extras = getIntent().getExtras();

		entityKey = extras.getString("entity_key");
		
		roomMap = cloudConnection.getRoomMap(entityKey);
		
		new GetRoomPic().execute(roomMap.get("img_url").toString());
		
		
		titleTextView.setText(roomMap.get("title").toString());
		descriptionTextView.setText(roomMap.get("description").toString());
		priceTextView.setText(roomMap.get("price").toString());
		
		
		houseTypeTextView.setText(roomMap.get("house_type").toString());
		roomTypeTextView.setText(roomMap.get("room_type").toString());
		bathRoomNoTextView.setText(roomMap.get("bathroom_count").toString());
		smokingTextView.setText(roomMap.get("smoking").toString());
		//preferredAgeTextView.setText(roomMap.get("age").toString());
		preferredGenderTextView.setText(roomMap.get("gender").toString());
		tvTextView.setText(roomMap.get("tv").toString());
		sky_cableTextView.setText(roomMap.get("sky_cable").toString());
		onsuiteTextView.setText(roomMap.get("onsuite").toString());
		internetTextView.setText(roomMap.get("internet").toString());
		heatingTextView.setText(roomMap.get("heating").toString());
		air_conTextView.setText(roomMap.get("air_con").toString());
		
		messageButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View v) {
				Intent in = new Intent(getApplicationContext(), Message.class);	
				in.putExtra("to",  roomMap.get("user").toString());
				in.putExtra("from", "byrnep37");
				in.putExtra("subject", roomMap.get("title").toString());
				startActivityForResult(in, 0);
			}
		});
		
		
		//imageView.setImageResource(roomMap.get("img_url").toString());
		
		
		
		
		
		//imageView.setImageBitmap((Bitmap) roomMap.get("photo"));
		
		
		
		//cloudConnection.setThumbnailImg(imageView, entityKey);
		//imageView.setImageBitmap(bm);
		
		  //setResult(RESULT_OK, intent); intent= getIntent(); String msg =
		  //intent.getStringExtra("message");
		 // Toast.makeText(getApplicationContext(), msg,
		 // Toast.LENGTH_SHORT).show(); msg +=" and hello from screen 2";
		  //intent.putExtra("returnedData", msg);
		  
		 // setResult(RESULT_OK, intent); finish();
		 
		
	}

	protected void onResume() {

		super.onResume();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	class GetRoomPic extends AsyncTask<String, Void, Bitmap> {

		@Override
		protected Bitmap doInBackground(String... url) {
			Bitmap bitmap = null;
			URL aURL;
			try {
				String tempUrl = url[0].toString();
				tempUrl = tempUrl.substring(0,tempUrl.length()-1) + "0";
				aURL = new URL(tempUrl);
				//aURL = new URL(imageUrl + "/" + json.get("room_img").toString());
				
				
				//aURL = new URL(json.get("pic_url").toString());
				bitmap = BitmapFactory.decodeStream(aURL.openStream());
				int i=3;
				int e=4;
				i+=e;
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			return bitmap;
		}
		
		
		@Override
	    // Once the image is downloaded, associates it to the imageView
	    protected void onPostExecute(Bitmap bitmap) {
			imageView.setImageBitmap(bitmap);
		}
		
	}
}
